<template>
  <div class="app-container">
    <div class="menu-container">
      <el-form :inline="true" style="float: left;">
        <el-button size="mini" type="primary" @click="addNewRecommend()">添加推荐</el-button>
      </el-form>
    </div>
    <el-table v-loading="listLoading" :data="page.records" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column width="80px" align="center" label="类型">
        <template slot-scope="scope">
          <span>{{ scope.row.type_desc }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="推荐对象名称">
        <template slot-scope="scope">
          <el-button type="text" size="small" icon="el-icon-edit" @click="modifyRecommend(scope.row)">
            {{ scope.row.recommend_name }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column width="120px" align="center" label="状态">
        <template slot-scope="scope">
          <span>{{ scope.row.state_desc }}</span>
        </template>
      </el-table-column>

      <el-table-column width="100px" align="center" label="显示顺序">
        <template slot-scope="scope">
          <span>{{ scope.row.show_order }}</span>
        </template>
      </el-table-column>

      <el-table-column width="100px" align="center" label="创建时间">
        <template slot-scope="scope">
          <span>{{ scope.row.create_time }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="Actions" width="240">
        <template slot-scope="scope">
          <!-- <el-button type="primary" size="small" icon="el-icon-edit" @click="modifyRecommend(scope.row)">修改</el-button> -->
          <el-button type="danger" size="small" icon="el-icon-delete" @click="delRecommend(scope.row)">删除</el-button>
          <el-button type="primary" size="small" icon="el-icon-tickets" @click="modifyRecommend(scope.row)">推荐单品管理</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="page.recordsTotal>0" :total="page.recordsTotal" :page.sync="page.pageNum" :page-sizes="[50, 100]" :limit.sync="page.pageSize" @pagination="getList" />

    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">

      <el-form :model="recommendForm">
        <el-form-item :label-width="formLabelWidth" label="推荐类型">
          <el-select v-model="recommendForm.type" placeholder="请选择" size="mini">
            <el-option
              v-for="item in recommendTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item v-if="recommendForm.type == 1" :label-width="formLabelWidth" label="推荐视频">
          <el-select v-model="recommendForm.video_id" filterable placeholder="请选择" size="mini">
            <el-option
              v-for="item in videoOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item v-else :label-width="formLabelWidth" label="推荐系列">
          <el-select v-model="recommendForm.serie_id" filterable placeholder="请选择" size="mini">
            <el-option
              v-for="item in serieOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="推荐名称">
          <el-input v-model="recommendForm.name" auto-complete="off" size="mini"/>
        </el-form-item>
        <el-form-item style="margin-bottom: 10px;" label="封面图">
          <Upload v-model="recommendForm.img_url" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="排序">
          <el-input-number v-model="recommendForm.show_order" :min="1" :max="100" size="mini" label="排序"/>
          <span>排序值越小排序越靠前</span>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="状态">
          <el-select v-model="recommendForm.state" placeholder="请选择" size="mini">
            <el-option
              v-for="item in stateOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogFormVisible = false">取 消</el-button>

        <el-button type="primary" size="mini" @click="createOrUpdateRecommend()">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { fetchSerieOptionsList } from '@/api/serie'
import { fetchList, createRecommend, updateRecommend, deleteRecommend } from '@/api/recommend'
import { fetchVideoOptions } from '@/api/video'
import Upload from '@/components/Upload/singleImage3'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'SerieList',
  components: { Pagination, Upload },
  data() {
    return {
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20
      },
      currentSerie: undefined,
      formLabelWidth: '100px',
      dialogFormVisible: false,

      dialogSerieVideoVisible: false,
      dialogTitle: '新增推荐',
      isEditSerie: false,
      recommendForm: { id: undefined, name: '', show_order: 1, state: 0, img_url: '', type: 1, serie_id: '', video_id: '' },
      recommendTypeOptions: [{ 'label': '视频', 'value': 1 }, { 'label': '系列', 'value': 2 }],
      stateOptions: [{ 'label': '有效', 'value': 0 }, { 'label': '无效', 'value': 1 }],
      videoOptions: [],
      serieOptions: [],
      optionForm: { 'id': undefined, 'show_order': 1, 'state': 0, 'img_url': '', 'serie_id': -1, 'video_id': '' },
      optionsPage: { pageNum: 1, pageSize: 100, pageTotal: 0, recordsTotal: 0, records: [] },
      page: { pageNum: 1, pageSize: 100, pageTotal: 0, recordsTotal: 0, records: [] }
    }
  },
  created() {
    this.getList()
    this.getListVideoOptions()
    this.getListSerieOptions()
  },
  methods: {
    clearSerieForm() {
      this.recommendForm = { id: undefined, name: '', show_order: 1, state: 0, img_url: '', type: 1, serie_id: '', video_id: '' }
      // this.recommendForm = { 'id': undefined, 'name': '', 'show_order': 1, 'state': 0 }
    },
    addNewRecommend() {
      this.dialogTitle = '新增推荐'
      this.dialogFormVisible = true
    },
    modifyRecommend(recommend) {
      this.recommendForm.id = recommend.id
      this.recommendForm.name = recommend.name
      this.recommendForm.img_url = recommend.img_url
      this.recommendForm.type = recommend.type
      this.recommendForm.video_id = recommend.video_id
      this.recommendForm.serie_id = recommend.serie_id
      this.recommendForm.show_order = recommend.show_order
      this.recommendForm.state = recommend.state
      this.dialogTitle = '修改推荐'
      this.dialogFormVisible = true
    },
    createOrUpdateRecommend() {
      let msg = '更新成功'
      let reqestFunc = updateRecommend
      if (this.recommendForm.id === undefined || this.recommendForm.id === '') {
        msg = '创建成功'
        reqestFunc = createRecommend
      }
      if (this.recommendForm.type === 1) {
        if (this.recommendForm.video_id === undefined || this.recommendForm.video_id === '') {
          this.$message({ message: '视频类型推荐必须选择一个视频', type: 'warning' })
          return
        }
        this.recommendForm.serie_id = ''
      } else {
        if (this.recommendForm.serie_id === undefined || this.recommendForm.serie_id === '') {
          this.$message({ message: '系列类型推荐必须选择一个系列', type: 'warning' })
          return
        }
        this.recommendForm.video_id = ''
      }
      reqestFunc(this.recommendForm).then(response => {
        const r = response.data
        if (r.code === 0) {
          this.$message({ message: msg, type: 'success' })
          this.dialogFormVisible = false

          this.clearSerieForm()
          this.getList()
        } else {
          this.$message({ message: r.msg, type: 'error' })
        }
      })
    },

    delRecommend(category) {
      const data = { id: category.id, type: 'serie' }
      this.myConfirm('即将删除推荐, 是否继续?', this._delRecommend, data, '已取消操作')
    },
    _delRecommend(data) {
      deleteRecommend(data.id).then(response => {
        const r = response.data
        if (r.code === 0) {
          this.$message({ message: '操作成功', type: 'success' })
          this.getList()
        } else {
          this.$message({ message: r.msg, type: 'error' })
        }
      })
    },
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        const data = response.data
        if (data.code !== 0) {
          this.$message.error(data.msg)
        }
        this.page = data.data
        this.listLoading = false
      })
    },
    getListVideoOptions() {
      this.listLoading = true
      fetchVideoOptions().then(response => {
        const data = response.data
        if (data.code !== 0) {
          this.$message.error(data.msg)
        }
        this.videoOptions = data.data.videos
        this.listLoading = false
      })
    },
    getListSerieOptions() {
      this.listLoading = true
      fetchSerieOptionsList().then(response => {
        const data = response.data
        if (data.code !== 0) {
          this.$message.error(data.msg)
        }
        this.serieOptions = data.data.series
        this.listLoading = false
      })
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
